﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BubbleSurvival
{
    public class PlayerSprite : AnimatedSprite
    {
        // Movement stuff
        MouseState prevMouseState;

        public PlayerState State { get; set; }
        public PlayerStatus Status { get; set; }

        // Get direction of sprite based on player input and speed
        public override Vector2 direction
        {
            get
            {
                Vector2 inputDirection = Vector2.Zero;

                // If player pressed arrow keys, move the sprite
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    inputDirection.X -= 1;
                    State = PlayerState.Walk;
                }
                if (Keyboard.GetState().IsKeyDown(Keys.A))
                {
                    inputDirection.X += 1;
                    State = PlayerState.Walk;
                }
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    inputDirection.Y -= 1;
                    State = PlayerState.Walk;
                }
                if (Keyboard.GetState().IsKeyDown(Keys.D))
                {
                    inputDirection.Y += 1;
                    State = PlayerState.Walk;
                }

                return inputDirection * speed;
            }
        }

        public PlayerSprite(Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame,
            sheetSize, speed)
        {
        }


        public PlayerSprite(Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed, int millisecondsPerFrame)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame,
            sheetSize, speed, millisecondsPerFrame)
        {
            Status = PlayerStatus.Normal;
            State = PlayerState.Idle;
        }

        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            MouseState currMouseState = Mouse.GetState();

            // Move the sprite based on direction
            position += direction;

            //TODO kurzorra fordulás

            switch (State)
            {
                case PlayerState.Idle :
                    sheetSize = Point.Zero;
                    break;
                case PlayerState.Walk:
                    sheetSize = new Point(0, 3);
                    break;
            }

            // Update frame if time to do so based on framerate
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > millisecondsPerFrame)
            {
                // Increment to next frame
                timeSinceLastFrame = 0;
                ++currentFrame.X;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.X = 0;
                    ++currentFrame.Y;
                    if (currentFrame.Y >= sheetSize.Y)
                        currentFrame.Y = 0;
                }
            }
        }

        enum PlayerState
        {
            Idle,
            Walk,
            Attack
        }

        enum PlayerStatus
        {
            Normal,
            Damaged,
            Poisoned
        }
    }
}
